import requests
import hashlib
import json
import time

def generate_sign(app_key, app_secret, timestamp):
    # 拼接字符串
    sign_str = f'{app_key}{app_secret}{timestamp}{app_secret}'
    # 使用MD5加密
    md5 = hashlib.md5()
    md5.update(sign_str.encode('utf-8'))
    return md5.hexdigest()

def query_access_token():
    # 接口信息
    # url = "http://localhost:8081/open/api/access/queryAccessToken"
    # app_key = 'sharing8'
    # app_secret = 'sharing8'
    
    url = "https://api.17donor.com/sharing/open/api/access/queryAccessToken"
    app_key = 'langfang'
    app_secret = 'd320a9e39ae03c63'
    timestamp = str(int(time.time())+20)

    # 生成签名
    sign = generate_sign(app_key, app_secret, timestamp)

    # 构建请求体
    payload = {
        'appKey': app_key,
        'timestamp': timestamp,
        'sign': sign
    }
    
    # 转为JSON字符串
    json_payload = json.dumps(payload)

    # 设置headers
    headers = {'Content-Type': 'application/json'}

    # 发送POST请求
    response = requests.post(url, data=json_payload, headers=headers)

    # 输出返回结果
    print("Response Code:", response.status_code)
    print("Response Content:", response.text)

if __name__ == "__main__":
    query_access_token()
